/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQSelectableUtils
{

	/**
	 * Create a JQSelectableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQSelectableBuilder
	 */
	public static JQSelectableBuilder create(String id, String key)
	{
		return new JQSelectableBuilder(new JQSelectable(id, (key == null ? JQSelectablePlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQSelectableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQSelectableBuilder
	 */
	public static JQSelectableBuilder create(String id)
	{
		return new JQSelectableBuilder(new JQSelectable(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQSelectableBuilder
	{
		/** JQuery component */
		private JQSelectable component;
		/** JQuery default options */
		private JQSelectableOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQSelectableBuilder(JQSelectable component)
		{
			this.component = component;
			this.options = new JQSelectableOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the auto refresh option value
		 * 
		 * @param autoRefresh
		 *            selectable auto refresh
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder autoRefresh(boolean autoRefresh)
		{
			options.setAutoRefresh(autoRefresh);
			return this;
		}

		/**
		 * Sets the filter option value
		 * 
		 * @param filter
		 *            selectable filter
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder filter(String filter)
		{
			options.setFilter(filter);
			return this;
		}

		/**
		 * Sets the selected function
		 * 
		 * @param selectedScript
		 *            selectable selected function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder selectedScript(String selectedScript)
		{
			options.setSelectedScript(selectedScript);
			return this;
		}

		/**
		 * Sets the selecting function
		 * 
		 * @param selectingScript
		 *            selectable selecting function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder selectingScript(String selectingScript)
		{
			options.setSelectingScript(selectingScript);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            selectable start function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            selectable stop function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * Sets the unselected function
		 * 
		 * @param unselectedScript
		 *            selectable unselected function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder unselectedScript(String unselectedScript)
		{
			options.setUnselectedScript(unselectedScript);
			return this;
		}

		/**
		 * Sets the unselecting function
		 * 
		 * @param unselectingScript
		 *            selectable unselecting function
		 * @return JQSelectableBuilder
		 */
		public JQSelectableBuilder unselectingScript(String unselectingScript)
		{
			options.setUnselectingScript(unselectingScript);
			return this;
		}

		/**
		 * @return JQSelectableBuilder
		 */
		public JQSelectable build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}